package com.itmk.web.goods_category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmk.web.goods.entity.Goods;
import com.itmk.web.goods.service.GoodsService;
import com.itmk.web.goods_category.entity.GoodsCategory;
import com.itmk.web.goods_category.mapper.GoodsCategoryMapper;
import com.itmk.web.goods_category.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements GoodsCategoryService {
    @Autowired
    private GoodsService goodsService;

    @Override
    public List<GoodsCategory> getCategoryGoodsCount() {
        // 获取所有分类
        List<GoodsCategory> categories = this.list();

        // 查询每个分类下的商品数量
        for (GoodsCategory category : categories) {
            QueryWrapper<Goods> query = new QueryWrapper<>();
            query.lambda().eq(Goods::getCategoryId, category.getCategoryId());
            int count = goodsService.count(query);
            category.setCount(count);
        }

        return categories;
    }
}
